package com.kisnow.security.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.Objects;

@WebFilter(urlPatterns = "/api/*")
public class CustomFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session = request.getSession();
        Object authorization = session.getAttribute("Authorization");
        System.out.println(authorization);
        if (Objects.isNull(authorization) || !String.valueOf(authorization).startsWith("Bearer")) {
            System.out.println("Session中未找到授权书！");
            return;
        }
        filterChain.doFilter(request, servletResponse);
    }
}
